import {createRouter, createWebHashHistory} from "vue-router";
import Home from "../views/Home.vue";
import CacheApi from '../utils/cache'
// import OtherApi from './other';
const routes = [
    {
        path: '/',
        redirect: '/dashboard'
    }, {
        path: "/",
        name: "Home",
        component: Home,
        children: [
            {
                path: "/dashboard",
                name: "dashboard",
                meta: {
                    title: '项目统计'
                },
                component: () => import ( /* webpackChunkName: "dashboard" */ "../views/Dashboard.vue")
            },
            {
                path:'/project_prepared',
                name:'ProjectPrepared',
                meta:{
                    title:'项目报备'
                },
                component:()=>import("../views/ProjectPrepared/ProjectPrepared.vue")
            },
            {
                path: '/user',
                name: 'user',
                meta: {
                    title: '个人中心'
                },
                component: () => import (/* webpackChunkName: "user" */ '../views/User.vue')
            },{
                path:'/customer',
                name:'customer',
                meta:{
                    title:'客户管理'
                },
                component:()=>import(/* webpackChunkName:"customer" */ '../views/Customer/list.vue')
            },{
                path:'/contract',
                name:'contract',
                meta:{
                    title:'合同管理'
                },
                component:()=>import(/* webpackChunkName:"contract"  */ '../views/Contract/Index.vue')
            }
            ,{
                path:'/dictionary',
                name:'dictionary',
                meta:{
                    title:'数据字典'
                },
                component:()=>import(/* webpackChunkName:"dictionary" */ '../views/Dictionary.vue')
            },
            {
                path:'/receivemoney',
                name:'receivemoney',
                meta:{
                    title:'收款管理'
                },
                component:()=>import('../views/ReceiveMoney/Index.vue')
            },
            {
                path:'/saler',
                name:'saler',
                meta:{
                    title:'销售人员管理'
                },
                component:()=>import('../views/Saler/Index.vue')
            },
            {
                path:'/product',
                name:'product',
                meta:{
                    title:'商品管理'
                },
                component:()=>import('../views/Product/Index.vue')
            },
            {
                path:'/invoice',
                name:'invoice',
                meta:{
                    title:'开票管理'
                },
                component:()=>import('../views/Invoice/Index.vue')
            }
            // ...OtherApi
        ],
        
    }, {
        path: "/login",
        name: "Login",
        meta: {
            title: '登录'
        },
        component: () => import ( /* webpackChunkName: "login" */ "../views/Login.vue")
    }
];

const router = createRouter({
    history: createWebHashHistory(),
    routes
});

router.beforeEach((to, from, next) => {
    document.title = `${to.meta.title} -- 朗德项目实施报备系统`;
    const role = CacheApi.token;
    console.log('获取当前TOKEN',CacheApi.token);
    if (!role && to.path !== '/login') {
        next('/login');
    } else if (to.meta.permission) {
        // 如果是管理员权限则可进入，这里只是简单的模拟管理员权限而已
        role === 'admin'
            ? next()
            : next('/403');
    } else {
        next();
    }
});

export default router;